package org.logistics.bean;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class getMapInforMethod {
	private String TAG = "org.logisitics.bean.getMapInforMethod";
	
	List<MapInforBean> mapInforList = new ArrayList<MapInforBean>();
	
	String userAccount ;
	
	public getMapInforMethod(String user ){
		userAccount = user;
		try {
			getLocalPlace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 
	 *请求服务器得到坐标信息对象
	 * @throws JSONException 
	 * @throws IOException 
	 * @throws IllegalStateException
	 */
	public List<MapInforBean> getLocalPlace() throws JSONException, IllegalStateException, IOException {
		BufferedReader in = null;
		/**
		 * 发送请求
		 */
		try {
			Log.i(TAG, "发出请求");
			String request = "http://sj.jy100.com/GetRealtime2.ashx?userName="+userAccount+"&address=true";
			HttpClient hc = new DefaultHttpClient();
			HttpPost hp = new HttpPost(request);
			HttpResponse response = hc.execute(hp);  
	        in = new BufferedReader(  
	                new InputStreamReader(  
	                        response.getEntity().getContent()));  
	        StringBuffer string = new StringBuffer("");  
	        String lineStr = "";  
	        while ((lineStr = in.readLine()) != null) {  
	            string.append(lineStr + "\n");  
	        }  
	        in.close();  
	        String resultStr = string.toString();  
	        Log.i(TAG, resultStr);
	        
	        /**
	         * 解析字符串
	         */
	        JSONObject jo = new JSONObject(resultStr);
	        
	        /**
	         * 得到数据条数
	         * TotalCount
	         */
	        int totalCount = jo.getInt("TotalCount");
	        Log.i(TAG, "共有"+totalCount+"条数据");
	        
	        /**
	         * 把解析数据转保存在对象
	         */
	        JSONArray jsonList = jo.getJSONArray("RootResults");
	        for(int i = 0 ; i < totalCount ; i++  )
	        {
	        	JSONObject temp = new JSONObject(jsonList.getString(i));
	        	MapInforBean mifb = new MapInforBean();
	        	mifb.setCarNum(temp.getString("A"));
	        	mifb.setiMei(temp.getString("D"));
	        	mifb.setOnLine(temp.getBoolean("F"));
	        	mifb.setLocationTime(temp.getString("G"));
	        	mifb.setH(temp.getDouble("H"));
	        	mifb.setI(temp.getDouble("I"));
	        	mifb.setAngle(temp.getInt("K"));
	        	mifb.setSpeed(temp.getInt("J"));
	        	mifb.setMileage(temp.getDouble("L"));
	        	mifb.setStatue(temp.getString("M"));
	        	mifb.setAddress(temp.getString("O"));
	        	mapInforList.add(mifb);
	        }
			
		} catch (JSONException e) {
			Log.i(TAG, e.getMessage());
		}finally {  
            if (in != null) {  
                try {  
                    in.close();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
            }  
        }
		
		return mapInforList;  
		
		
	}

}
